package weaver.interfaces.cetc.duss.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.concurrent.TimeUnit;

/**
 * @author dusongsong
 * @date 2024年04月23日 16:17
 */
public class CetcUtil {

    // 发送http请求
    public static JSONObject sendOKHttpRequest(String paramJson, String url) {
        JSONObject resultJson = new JSONObject();
        String data = "";
        String message = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, paramJson);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }
    // 根据用户id获取登录名
    public static String getLoginidById(String usrid){
        RecordSet rs = new RecordSet();
        rs.execute("select loginid from hrmresource where id=?",usrid);
        rs.next();
        return Util.null2String(rs.getString("loginid"));
    }
}
